Skip to content

Added Windows support #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

bedware
Copy link

@bedware bedware commented Jun 26, 2023

Trying to solve that issue - #4

@bedware
Copy link
Author

bedware commented Jun 26, 2023

I have managed to run several commands like:

  • Info
  • Choose Port
  • Verify
  • Upload
  • Monitor Serial port
  • Others can also work, I didn't check

I'm using Windows 10.

Some requirements to use this patch:

  • pwsh installed (cross-platform version 7+)
  • arduino-cli installed

Unfortunately, I can't test on a clean system and am afraid of side effects that may happen. I would appreciate any help with testing.

I do this in Draft because I don't know Lua and I hope someone can help me review my Lua code. Especially why I just can't make my nested substitute multiline? :\

@@ -47,6 +47,11 @@ function! arduino#InitializeConfig() abort
endif
if !exists('g:arduino_serial_cmd')
let g:arduino_serial_cmd = 'screen {port} {baud}'
if s:OS ==? 'Windows'
let g:arduino_serial_cmd = 'arduino-cli monitor -p {port} --config baudrate={baud}'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the arduino-cli supports reading the serial output now, we should just use this as the new default (provided s:has_cli)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is supported. In fact, I didn't know it wasn't supported before. I think it is a good idea to use it as default.

Comment on lines 656 to 661
if s:OS ==? 'Windows'
let found = split(system('pwsh -nop -c "arduino-cli board list | Select-String -Pattern \"^(COM\d) \" | ForEach-Object { $_.Matches.Value }"'), '\n')
for port in found
call add(ports, port)
endfor
endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if you could call arduino-cli directly so it doesn't rely on powershell. Do the ports appear in the json data if you run it like we do here?

let boards_data = json_decode(system('arduino-cli board listall --format json'))

If not, you could still just call arduino-cli board list and the extract the information you want using matchlist().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. I'm gonna change it.

Comment on lines +203 to +204
let l:path = substitute(l:path, '{file}', substitute(expand('%:p'), '\', '/', 'g'), 'g')
let l:path = substitute(l:path, '{project_dir}', substitute(expand('%:p:h'), '\', '/', 'g'), 'g')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you mean when you say the nested substitutes are not working? What specifically isn't working about them?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for the confusion. Nested substitutions work fine. I will write another message about what is working as I not expecting.

I used this hack to make it work on Windows. I don't know why any path on Windows is composed using backslashes. Without changing slashes topmost substitute interpreted '\U' sequence as a trigger to make the next characters UPPERCASE. But on Windows path quite often starts with the C:/User/<username> prefix. That broke compiling and uploading.

@bedware
Copy link
Author

bedware commented Jun 28, 2023

What I meant is that subs(subs( work as expected, but when I tried to make nested subs on the new line - it stop working. I think It is due to my limited knowledge of Lua.

# works
  let l:path = substitute(l:path, '{file}', substitute(expand('%:p'), '\', '/', 'g'), 'g')

# doesn't work
  let l:path = substitute(l:path, '{file}', substitute(
    expand('%:p'), '\', '/', 'g'), 'g')

@bedware bedware marked this pull request as ready for review June 28, 2023 17:09
@bedware
Copy link
Author

bedware commented Jun 28, 2023

Also I noticed that <leader>aus doesn't work. But <leader>as works fine. Here is the output:

Writing | ################################################## | 100% 0.77s

avrdude: 2514 bytes of flash written

avrdude done.  Thank you.


^[[92mUsed platform^[[0m        ^[[92mVersion^[[0m ^[[90mPath^[[0m

^[[93matmel-avr-xminis:avr^[[0m 0.6.0   ^[[90mC:\Users\dmitr\AppData\Local\Arduino15\packages\atmel-avr-xminis\hardware\avr\0.6.0^[[0m
^[[93marduino:avr^[[0m          1.8.6   ^[[90mC:\Users\dmitr\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6^[[0m

:!arduino-cli monitor -p COM4 --config baudrate=9600
not running in a terminal

shell returned 1

Do you have any ideas on how to solve this? Looks like aus somehow depends on terminal commands, but not using just regular as command. Am I right? I saw something in the code, but I need further investigation.

@stevearc
Copy link
Owner

What version of vim are you using? We're running the command like so

exe s:TERM . a:cmd

and s:TERM is set via this logic

if has('nvim')
let s:TERM = 'botright split | terminal! '
elseif has('terminal')
" In vim, doing terminal! will automatically open in a new split
let s:TERM = 'terminal! '
else
" Backwards compatible with old versions of vim
let s:TERM = '!'
endif

Both of the terminal options should run the command inside a terminal. From the logs you pasted, it looks like you might have just been doing a raw shell execution :!<cmd>

@bedware
Copy link
Author

bedware commented Jul 12, 2023

Upload & Serial works for me now and it is a much better experience.
Now all general things are working. How we can publish this? Do I need to make some tests or something else?
I really want to publish this =)

@bedware
Copy link
Author

bedware commented Jul 12, 2023

What version of vim are you using? We're running the command like so

NVIM v0.9.1

@bedware bedware marked this pull request as draft July 12, 2023 18:56
@bedware bedware changed the title Draft: Added Windows support Added Windows support Jul 12, 2023
@bedware bedware marked this pull request as ready for review July 12, 2023 19:00
Copy link
Owner

@stevearc stevearc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Couple of small refactors requested

Comment on lines +78 to +80
if g:arduino_use_cli
let g:arduino_serial_cmd = 'arduino-cli monitor -p {port} --config baudrate={baud}'
endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this into the if !exists('g:arduino_serial_cmd') above (line 49). Move that check below this one, and then set it to be the default serial command if g:arduino_use_cli

Comment on lines +653 to +658
if s:OS ==? 'Windows'
let boards_data = json_decode(system('arduino-cli board list --format json'))
for board in boards_data
call add(ports, board['port']['address'])
endfor
endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be windows-only? It seems like this API should work on all platforms. We should put it behind a if g:arduino_use_cli check, though. And also deduplicate any entries in the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants